package com.doucat.support.component.fragment.base;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.doucat.support.component.DateInterface;
import com.doucat.support.view.CatButton;
import com.tonly.doucatlibrary.R;

import java.util.Calendar;

/**
 * -----------------------------------------------------------------
 * Copyright (C) 2021-2031, by tonly, All rights reserved.
 * -----------------------------------------------------------------
 * File: DataFragment.java
 * Author: wangfy
 * Create: 2021/7/13 10:55
 *
 * 日期区间选择
 */
public class DateFragment extends ToolbarFragment implements DateInterface {
    private int amount=1;
    public StringBuilder startTime;
    public StringBuilder endTime;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Calendar calendar= Calendar.getInstance();
        //当天的时间
        endTime=new StringBuilder();
        endTime
                .append(calendar.get(Calendar.YEAR)+"-")
                .append(format(calendar.get(Calendar.MONTH)+1)+"-")
                .append(format(calendar.get(Calendar.DATE)));
        //7天前的时间
        startTime=new StringBuilder();
        calendar.add(Calendar.DATE,-amount);
        startTime
                .append(calendar.get(Calendar.YEAR)+"-")
                .append(format(calendar.get(Calendar.MONTH)+1)+"-")
                .append(format(calendar.get(Calendar.DATE)));
    }

    public String format(int time) {
        String str = "" + time;
        if(time<10){
            str = "0" + time;
        }
        return str;
    }

    public void setAmount(int amount) {
        this.amount = amount;
    }
    private ViewGroup viewGroupDate;
    private ViewGroup viewGroupDateBox;
    private FrameLayout frameDateContent;
    private TextView tvStartTime;
    private TextView tvEndTime;
    private CatButton btnSearch;
    @Override
    public void onRootViewCreate(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onRootViewCreate(inflater,container,savedInstanceState);
        setTitleFrameLayout(R.layout.fragment_date);
        viewGroupDate = findViewById(R.id.viewGroupDate);
        viewGroupDateBox = findViewById(R.id.viewGroupDateBox);
        frameDateContent = findViewById(R.id.frameDateContent);
        tvStartTime = findViewById(R.id.tvStartTime);
        tvEndTime = findViewById(R.id.tvEndTime);
        btnSearch = findViewById(R.id.btnSearch);
        tvStartTime.setText(startTime);
        tvEndTime.setText(endTime);
        viewGroupDateBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Calendar calendar= Calendar.getInstance();
                new DatePickerDialog(getContext(),
                        new DatePickerDialog.OnDateSetListener() {
                            @Override
                            public void onDateSet(DatePicker view, int year, int month, int day) {
                                Log.d(TAG, "您选择了：" + year + "年" + (month + 1)
                                        + "月" + day + "日");
                                StringBuilder startBuilder = new StringBuilder()
                                        .append(year).append("-")
                                        .append(format(month + 1)).append("-")
                                        .append(format(day));
                                startTime=startBuilder;
                                tvStartTime.setText(startTime.toString());
                            }
                        }
                        , calendar.get(Calendar.YEAR)
                        , calendar.get(Calendar.MONTH)
                        , calendar.get(Calendar.DATE)
                ).show();
            }
        });
        tvEndTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Calendar calendar= Calendar.getInstance();
                new DatePickerDialog(getContext(),
                        new DatePickerDialog.OnDateSetListener() {
                            @Override
                            public void onDateSet(DatePicker view, int year, int month, int day) {
                                Log.d(TAG, "您选择了：" + year + "年" + (month + 1)
                                        + "月" + day + "日");
                                StringBuilder endBuilder = new StringBuilder()
                                        .append(year).append("-")
                                        .append(format(month + 1)).append("-")
                                        .append(format(day));
                                endTime=endBuilder;
                                tvEndTime.setText(endTime.toString());
                            }
                        }
                        , calendar.get(Calendar.YEAR)
                        , calendar.get(Calendar.MONTH)
                        , calendar.get(Calendar.DATE)
                ).show();
            }
        });

        btnSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dateRequest(startTime.toString(), endTime.toString());
            }
        });
    }

    public void setDateFragmentContentView(int layoutResID) {
        frameDateContent.removeAllViews();
        View.inflate(getContext(), layoutResID, frameDateContent);
    }

    @Override
    public void dateRequest(String startTime, String endTime) {

    }

    /**
     * 隐藏日期条
     * 但参数可以使用
     */
    public void hideDateLayout() {
        viewGroupDate.setVisibility(View.GONE);
    }
}